package com.taotao.service.com.taotao.service.Impl;

import com.taotao.TaotaoResult;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.pojo.TreeNode;
import com.taotao.service.ContentCatgoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by mafuhua on 2017/9/29.
 */
@Service
public class ContentCatgoryServiceImpl implements ContentCatgoryService {
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Override
    public List<TreeNode> getContentCatList(long parentId) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> tbContentCategories =
                tbContentCategoryMapper.selectByExample(example);
        ArrayList<TreeNode> treeList = new ArrayList<>();
        for (TbContentCategory contentCategory : tbContentCategories) {
            if (contentCategory.getStatus() == 1) {
                TreeNode treeNode = new TreeNode();
                treeNode.setId(contentCategory.getId());
                treeNode.setState(contentCategory.getIsParent() ? "closed" : "open");
                treeNode.setText(contentCategory.getName());
                treeList.add(treeNode);
            }

        }
        return treeList;
    }

    public List<TbContentCategory> getCatList(long parentId) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> tbContentCategories =
                tbContentCategoryMapper.selectByExample(example);

        return tbContentCategories;
    }

    @Override
    public TaotaoResult insertCategory(long parentId, String name) {
        TbContentCategory contentCategory = new TbContentCategory();
        Date date = new Date();
        contentCategory.setCreated(date);
        contentCategory.setUpdated(date);
        contentCategory.setParentId(parentId);
        contentCategory.setName(name);
        contentCategory.setSortOrder(1);
        contentCategory.setStatus(1);
        contentCategory.setIsParent(false);

        tbContentCategoryMapper.insert(contentCategory);

        Long id = contentCategory.getId();

        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(parentId);
        if (!tbContentCategory.getIsParent()) {
            tbContentCategory.setIsParent(true);
            tbContentCategoryMapper.updateByPrimaryKey(tbContentCategory);
        }

        return TaotaoResult.ok(id);
    }

    @Override
    public TaotaoResult updateCategory(long id, String name) {
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(id);
        tbContentCategory.setName(name);
        tbContentCategoryMapper.updateByPrimaryKey(tbContentCategory);
        return TaotaoResult.ok(id);
    }

    public void setDel(TbContentCategory tbContentCategory) {
        if (tbContentCategory.getIsParent()) {
            List<TbContentCategory> contentCatList = getCatList(tbContentCategory.getId());
            for (TbContentCategory category : contentCatList) {
                if (category.getIsParent()) {
                    setDel(category);
                }
                category.setStatus(2);
                tbContentCategoryMapper.updateByPrimaryKey(category);
            }
        }
        tbContentCategory.setStatus(2);
        tbContentCategoryMapper.updateByPrimaryKey(tbContentCategory);
    }

    @Override
    public TaotaoResult deleteCategory(long parentId, long id) {
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(id);
        setDel(tbContentCategory);
        return TaotaoResult.ok(id);
    }
}
